﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NS_DBOP;
using System.IO;
using System.Data.SqlClient;

public partial class Flow_NewApply : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataSet ds = new DBoperate().ExecuteQuery("select * from FlowType", 0, 0, "flowtype");
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                this.drpFltype.Items.Add(new ListItem(dr["Name"].ToString(), dr["ID"].ToString()));
            }
            this.drpFltype.Items.Insert(0, "请选择流程类别");
            if (Request.QueryString["id"] != null)
            {
                DataSet dsfd = new DBoperate().ExecuteQuery("select * from FlowDoc where ID=" + Convert.ToInt32(Request.QueryString["id"]), 0, 0, "flowdoc");
                foreach (DataRow dr in dsfd.Tables[0].Rows)
                {
                    this.txtTitle.Text = dr["Title"].ToString();
                    this.HtmlEditor1_HtmlEditorContent.Value = dr["Content"].ToString();
                }
            }
        }


    }
    protected void drpFltype_SelectedIndexChanged(object sender, EventArgs e)
    {
        string type = this.drpFltype.SelectedItem.Value;
        DataSet ds = new DBoperate().ExecuteQuery("select * from Flow where TypeID=" + Convert.ToInt32(type), 0, 0, "flow");
        
        this.drpFlow.DataSource = ds.Tables[0];
            this.drpFlow.DataTextField = "Name";
            this.drpFlow.DataValueField = "ID";
            this.drpFlow.DataBind();
       
    }
    protected void Submit1_ServerClick(object sender, EventArgs e)
    {
        string title = this.txtTitle.Text;
        int urgent = Convert.ToInt32(this.drpUrgent.SelectedItem.Value);
        int flowid =Convert.ToInt32(this.drpFlow.SelectedItem.Value);
        int userid=Convert.ToInt32(new DBoperate().ExecuteScalar("select Employeeid from Employee where username='"+Session["UserName"]+"'"));
        //string FileName = this.FileUpload1.PostedFile.FileName;
        string content = this.HtmlEditor1_HtmlEditorContent.Value.ToString();
        int result = 0;
        string src="";
        if (Request.QueryString["id"] != null)
        {
            result = new DBoperate().ExecuteUpdate("update FlowDoc set Title='" + title + "',Content='" + content + "'");
            if (result > 0)
            {
                Response.Write("<script language=javascript>alert('保存成功！');</script>");
            }
            else
            {
                Response.Write("<script language=javascript>alert('保存失败!');</script>");
            }
        }
        else
        {
            if (!Directory.Exists(Server.MapPath(".") + "\\Files\\"))
            {
                Directory.CreateDirectory(Server.MapPath(".") + "\\Files\\");
            }

             if (FileUpload1.PostedFile.ContentLength == 0)
            {
                src = "";
            }
            else
            {

                string strFilePath = FileUpload1.PostedFile.FileName;
                FileInfo fl = new FileInfo(strFilePath);
                string Ext = fl.Name;

                src = Ext;
                string ServerPath = Server.MapPath(".");
                string strSeraPath = ServerPath + "\\Files\\" + src;
                FileUpload1.PostedFile.SaveAs(strSeraPath);
                string strSql = "insert into FlowDoc values('" + title + "'," + urgent + "," + flowid + ",1," + userid + ",'" + content + "',default,1,0,'" + src + "')";
                result = new DBoperate().ExecuteUpdate(strSql);
            }

            if (result > 0)
            {
                Response.Write("<script language=javascript>alert('保存成功！');</script>");
            }
            else
            {
                Response.Write("<script language=javascript>alert('保存失败!');</script>");
            }
        }
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        //插入申请
        string title = this.txtTitle.Text;
        int urgent = Convert.ToInt32(this.drpUrgent.SelectedItem.Value);
        int flowid = Convert.ToInt32(this.drpFlow.SelectedItem.Value);
        int userid = Convert.ToInt32(new DBoperate().ExecuteScalar("select Employeeid from Employee where username=" + Session["UserName"]));
        //string FileName = this.FileUpload1.PostedFile.FileName;
        string content=this.HtmlEditor1_HtmlEditorContent.Value.ToString();
        int result = 0;
        string src = "";
        if (Request.QueryString["id"] != null)
        {
            result = new DBoperate().ExecuteUpdate("update FlowDoc set Title='" + title + "',Content='" + content + "',IsSave=0");
            int approve = Convert.ToInt32(new DBoperate().ExecuteScalar("select top 1 Num from Path where FlowID=" + Convert.ToInt32(Request.QueryString["id"]) + " order by Num desc"));
            int user = Convert.ToInt32(new DBoperate().ExecuteScalar("select Employeeid from Employee where username='" + Session["UserName"].ToString() + "'"));
            int num = Convert.ToInt32(new DBoperate().ExecuteScalar("select Num from Path where UserID=" + user));
            DataSet ds = new DBoperate().ExecuteQuery("select * from Path where FlowID=" + flowid, 0, 0, "path");
            //int result = 0;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (Convert.ToInt32(dr["UserID"]) == userid)
                {
                    string strSql = "insert into FlowPath (DocID,FlowID,StepID,UserID,IsApprove,Num,IsJoin) values(" + Convert.ToInt32(Request.QueryString["id"]) + "," +
                    Convert.ToInt32(dr["FlowID"]) + "," + Convert.ToInt32(dr["ID"]) + "," + dr["UserID"].ToString() + "," + (approve - Convert.ToInt32(dr["Num"])) + "," + dr["Num"].ToString() + "," + dr["IsJoin"].ToString() + ")";
                    result = new DBoperate().ExecuteUpdate(strSql);
                    continue;
                }
                if (Convert.ToInt32(dr["Num"]) > num)
                {
                    string strsql = "insert into FlowPath (DocID,FlowID,StepID,UserID,IsApprove,Num,IsJoin) values(" + Convert.ToInt32(Request.QueryString["id"]) + "," +
                        Convert.ToInt32(dr["FlowID"]) + "," + Convert.ToInt32(dr["ID"]) + "," + dr["UserID"].ToString() + "," + (approve - Convert.ToInt32(dr["Num"])) + "," + dr["Num"].ToString() + "," + dr["IsJoin"].ToString() + ")";
                    result = new DBoperate().ExecuteUpdate(strsql);
                }
            }
        }
        else
        {
            if (!Directory.Exists(Server.MapPath(".") + "\\Files\\"))
            {
                Directory.CreateDirectory(Server.MapPath(".") + "\\Files\\");
            }

            if (FileUpload1.PostedFile.ContentLength == 0)
            {
                src = "";
            }
            else
            {

                string strFilePath = FileUpload1.PostedFile.FileName;
                FileInfo fl = new FileInfo(strFilePath);
                string Ext = fl.Name;

                src = Ext;
                string ServerPath = Server.MapPath(".");
                string strSeraPath = ServerPath + "\\Files\\" + src;
                FileUpload1.PostedFile.SaveAs(strSeraPath);
                string strSql = "insert into FlowDoc values('" + title + "'," + urgent + "," + flowid + ",1," + userid + ",'" + content + "',default,1,0,'" + src + "')";
                result = new DBoperate().ExecuteUpdate(strSql);
            }


            //插入申请审批路径
            int docid = Convert.ToInt32(new DBoperate().ExecuteScalar("select top 1 ID from FlowDoc order by PubDate desc"));
            int approve = Convert.ToInt32(new DBoperate().ExecuteScalar("select top 1 Num from Path where FlowID=" + flowid + " order by Num desc"));
            int user = Convert.ToInt32(new DBoperate().ExecuteScalar("select Employeeid from Employee where username='" + Session["UserName"].ToString() + "'"));
            int num = Convert.ToInt32(new DBoperate().ExecuteScalar("select Num from Path where UserID=" + user));
            DataSet ds = new DBoperate().ExecuteQuery("select * from Path where FlowID=" + flowid, 0, 0, "path");
            //int result = 0;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (Convert.ToInt32(dr["UserID"]) == userid)
                {
                    string strSql = "insert into FlowPath (DocID,FlowID,StepID,UserID,IsApprove,Num,IsJoin) values(" + docid + "," +
                    Convert.ToInt32(dr["FlowID"]) + "," + Convert.ToInt32(dr["ID"]) + "," + dr["UserID"].ToString() + "," + (approve - Convert.ToInt32(dr["Num"])) + "," + dr["Num"].ToString() + "," + dr["IsJoin"].ToString() + ")";
                    result = new DBoperate().ExecuteUpdate(strSql);
                    continue;
                }
                if (Convert.ToInt32(dr["Num"]) > num)
                {
                    string strsql = "insert into FlowPath (DocID,FlowID,StepID,UserID,IsApprove,Num,IsJoin) values(" + docid + "," +
                        Convert.ToInt32(dr["FlowID"]) + "," + Convert.ToInt32(dr["ID"]) + "," + dr["UserID"].ToString() + "," + (approve - Convert.ToInt32(dr["Num"])) + "," + dr["Num"].ToString() + "," + dr["IsJoin"].ToString() + ")";
                    result = new DBoperate().ExecuteUpdate(strsql);
                }
            }
        }
       
       }
}
